package com.vertx.verticles;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.mongo.FindOptions;
import io.vertx.ext.mongo.MongoClient;

import java.util.ArrayList;
import java.util.List;

/**
 * @name: MongoVerticle
 * @description: MongoVerticle
 * @author: jiangcs
 */
public class MongoVerticle extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        JsonObject config = new JsonObject();
        config.put("connection_string", "mongodb://qdhlm:qdhlm.@192.168.0.158:27017");
        config.put("db_name", "dts");
        config.put("username", "qdhlm");
        config.put("password", "qdhlm.");
        MongoClient mongoClient = MongoClient.createShared(vertx, config);

        JsonObject query = new JsonObject();
        query.put("name", "test");
        mongoClient.find("person", query, (res) -> {
            if (res.succeeded()) {
                List<JsonObject> resultList = res.result();
                System.out.println("query ok:" + resultList.size());
                for (JsonObject resultObj : resultList) {
                    System.out.println(resultObj);
                }
            } else {
                res.cause().printStackTrace();
            }
        });

        JsonObject personOne = new JsonObject();
        personOne.put("name", "test");
        personOne.put("age", 21);
        personOne.put("score", 98.8);
        personOne.put("actors", new ArrayList<>());
        mongoClient.save("person", personOne)
                .onSuccess((id) -> {
                    System.out.println("onSuccess1:" + id);
                })
                .onFailure(Throwable::printStackTrace)
                .onComplete((res) -> {
                    if (res.succeeded()) {
                        System.out.println("onComplete1:" + res.result());
                    } else {
                        res.cause().printStackTrace();
                    }
                });

        JsonObject personTwo = new JsonObject();
        personTwo.put("name", "test1");
        personTwo.put("age", 24);
        personTwo.put("score", 88.8);
        personTwo.put("actors", new ArrayList<>());
        mongoClient.save("person", personTwo)
                .compose((id) -> mongoClient.findOne("person", new JsonObject().put("_id", id), new JsonObject().put("age", 1)))
                .onSuccess((obj) -> {
                    System.out.println("onSuccess2:" + obj);
                })
                .onFailure(Throwable::printStackTrace)
                .onComplete((res) -> {
                    if (res.succeeded()) {
                        System.out.println("onComplete2:" + res.result());
                    } else {
                        res.cause().printStackTrace();
                    }
                });

        FindOptions findOptions = new FindOptions();
        findOptions.setSort(new JsonObject().put("age", 1));
        mongoClient.findWithOptions("person", new JsonObject(), findOptions)
                .onSuccess((obj) -> {
                    System.out.println("onSuccess3:" + obj);
                })
                .onFailure(Throwable::printStackTrace)
                .onComplete((res) -> {
                    if (res.succeeded()) {
                        System.out.println("onComplete3:" + res.result());
                    } else {
                        res.cause().printStackTrace();
                    }
                });
    }
}
